const express = require('express');
const cors = require('cors');
const dotenv = require('dotenv');

// 加载环境变量
dotenv.config();

// 创建Express应用
const app = express();

// 中间件
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 路由
const authRoutes = require('./routes/auth.routes');
const chatRoutes = require('./routes/chat.routes');
const difyRouter = require('./routes/dify.route');
const videoRouter = require('./routes/video.route');
const wordRouter = require('./routes/word.route');
const pronunciationRoutes = require('./routes/pronunciation');
const speechRoutes = require('./routes/speech.routes');
app.use('/api/auth', authRoutes);
app.use('/api/chat', chatRoutes);
app.use('/api/dify', difyRouter);
app.use('/api/video', videoRouter);
app.use('/api/word', wordRouter);
app.use('/api/pronunciation', pronunciationRoutes);
app.use('/api/speech', speechRoutes);

// 添加静态文件服务
app.use('/uploads', express.static('public/uploads'));

// 基础路由
app.get('/', (req, res) => {
  res.json({ message: 'Welcome to AI English Teaching API' });
});

// 错误处理中间件
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({
    status: 'error',
    message: 'Internal Server Error'
  });
});

// 启动服务器
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

module.exports = app; 